import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:fluttertoast/fluttertoast.dart';
import 'package:member_center_sdk/sdk.dart';
import 'package:member_center_sdk_example/core/auth_type.dart';
import 'action.dart';
import 'state.dart';

Effect<SetPasswordByOldState> buildEffect() {
  return combineEffects(<Object, Effect<SetPasswordByOldState>>{
    SetPasswordByOldAction.setPassword: _onSetPasswordAction,
  });
}

Future _onSetPasswordAction(
    Action action, Context<SetPasswordByOldState> ctx) async {
  var newPassword = ctx.state.newPwdController.text;
  if (newPassword == null || newPassword.isEmpty) {
    Fluttertoast.showToast(msg: "请填写新的密码");
    return;
  }
  var oldPassword = ctx.state.oldPwdController.text;
  if (oldPassword == null || oldPassword.isEmpty) {
    Fluttertoast.showToast(msg: "请填写原密码");
    return;
  }
  var sdk = MemberCenterSdk.get();
  var sdkResult = await sdk.setPassword(
      AuthType.login.index, oldPassword, "", "", newPassword);
  if (sdkResult.success) {
    if (sdkResult.value == true) {
      Fluttertoast.showToast(msg: "修改密码成功");
      Navigator.of(ctx.context).pop(true);
    } else {
      Fluttertoast.showToast(msg: "修改密码失败");
    }
  } else {
    Fluttertoast.showToast(msg: sdkResult.message);
  }
}
